<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/11/5
 * Time: 9:16
 * 用户评论审核管理
 */

namespace Admin\Controller;

use Think\Image;
use Think\Upload;

class CommentController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 评论列表
     */
    public function index()
    {
        $wadmin = session("wadmin");//当前店铺管理员信息
        if ($wadmin['store_id'] == 0) {
            $map = '';
        } else {
            $map['c.store_id'] = $wadmin['store_id'];
        }

        $count = M("comment")->alias("c")->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出
        $result = M("comment")->alias("c")
            ->join("meal_store as s on c.store_id=s.store_id")
            ->field("c.*,s.store_name")
            ->where($map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("c.id desc")
            ->select();

//        dump($result);die;

        //取评论订单中所有菜品名称
        $dishes_arr = [];
        foreach ($result as $key => $val) {
            //将dishes_ids转为数组
            $result[$key]['dishes_ids'] = explode(',', $val['dishes_ids']);
            foreach ($result[$key]['dishes_ids'] as $k => $v) {
                $result[$key]['dishes_ids'][$k] = M('dishes')->where(['id' => $v])->getField('name');
            }
            $result[$key]['dishes'] = implode(',', $result[$key]['dishes_ids']);
        }

//        dump($result);die;

        $this->assign("result", $result);
        $this->assign("page", $show); // 赋值分页输出
        $this->display();
    }

    /**
     * 审核
     */
    public function examine()
    {
        //首次进入页面传过来的ID
        $id = I('get.id');
        $examine_result = M('comment')->where("id='$id'")->find();
        if (IS_POST) {
            $remark = I('post.remark');
            $examine = I('post.adopt');
            $newid = I('post.id');

            if ($examine == 1) {//通过审核
                $data['examine'] = 1;
                $data['time'] = time();
                $data['remark'] = '';
                $result = M('comment')->where("id='$newid'")->setField($data);
                if ($result === FALSE) {
                    $this->error('设置失败请重试');
                } elseif ($result === 0) {
                    $this->error("未修改");
                } else {
                    $this->success('审核通过', U('Admin/Comment/index'));
                    exit;
                }
            } elseif ($examine == '2') {//不通过审核
                if ($remark != '') {
                    $data['examine'] = -1;
                    $data['remark'] = $remark;
                    $data['time'] = time();
                    //表单提交的ID
                    $result = M('comment')->where("id='$newid'")->save($data);
                    if ($result === FALSE) {
                        $this->error('设置失败请重试');
                    } elseif ($result === 0) {
                        $this->error("未修改");
                    } else {
                        $this->success("审核不通过", U('Admin/Comment/index'));
                        exit;

                    }
                } else {
                    $this->error("请填写未通过原因");
                }
            } else {
                $this->error("网络错误,请重试");
            }

        }
        $this->assign('examine_result', $examine_result);
        $this->assign("id", $id);
        $this->display();

    }

    /**
     * 删除
     */
    public function del()
    {
        $id = I('request.id');
        $result = M('comment')->where("id='$id'")->delete();
        if ($result) {
            $this->success("删除成功", U('Admin/Comment/index'));
        } else {
            $this->error("删除失败");
        }
    }

}